﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using Utilities;

namespace WordAddin
{
    public partial class Encryptfrm :DevComponents.DotNetBar.Office2007Form
    {

        public SymmetricAlgorithm EncryptionAlgorithm { get; set; }
        public PaddingMode PaddingMode { get; set; }
        public CipherMode CipherMode { get; set; }
        public User Receiver { get; set; }
        public bool JustMe { get; set; }
        public string OutputPath { get; set; }
        public bool ShareToFriend { get; set; }

        public Encryptfrm(User receiver)
        {
            InitializeComponent();
            if (receiver == null)
            {
                btnViewFriendInfos.Enabled = false;
                radioButtonShare.Enabled = false;
            }
            else
            {
                radioButtonShare.Enabled = true;
            }

            cbbAlgorithm.SelectedIndex = 0;
            cbbPadding.SelectedIndex = 0;
            cbbCipher.SelectedIndex = 0;

            EncryptionAlgorithm = new DESCryptoServiceProvider();
            PaddingMode = PaddingMode.None;
            CipherMode = CipherMode.CBC;
            Receiver = receiver;
        }

        private void btnViewFriendInfos_Click(object sender, EventArgs e)
        {
            FriendInfoFrm frm = new FriendInfoFrm(Receiver);
            frm.ShowDialog();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            saveFileDialog1.Title = "Save to file ... ";
            saveFileDialog1.Filter = "Encrypted file|*.enc";
            saveFileDialog1.FileName = string.Empty;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                tbOutput.Text = saveFileDialog1.FileName;
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            switch (cbbAlgorithm.SelectedIndex)
            {
                case 0:
                    EncryptionAlgorithm = new DESCryptoServiceProvider();
                    break;
                case 1:
                    EncryptionAlgorithm = new TripleDESCryptoServiceProvider();
                    break;
                case 2:
                    EncryptionAlgorithm = new RC2CryptoServiceProvider();
                    break;
                case 3:
                    EncryptionAlgorithm = new RijndaelManaged();
                    break;
            }

            switch (cbbPadding.SelectedIndex)
            {
                case 0:
                    PaddingMode = PaddingMode.None;
                    break;
                case 1:
                    PaddingMode = PaddingMode.PKCS7;
                    break;
                case 2:
                    PaddingMode = PaddingMode.Zeros;
                    break;
                case 3:
                    PaddingMode = PaddingMode.ANSIX923;
                    break;
                case 4:
                    PaddingMode = PaddingMode.ISO10126;
                    break;
            }

            switch (cbbCipher.SelectedIndex)
            {
                case 0:
                    CipherMode = CipherMode.CBC;
                    break;
                case 1:
                    CipherMode = CipherMode.ECB;
                    break;
                case 2:
                    CipherMode = CipherMode.OFB;
                    break;
                case 3:
                    CipherMode = CipherMode.CFB;
                    break;
                case 4:
                    CipherMode = CipherMode.CTS;
                    break;
            }

            JustMe = radioButtonMe.Checked;
            ShareToFriend = radioButtonShare.Checked;

            OutputPath = tbOutput.Text;

            DialogResult = DialogResult.OK;
            this.Close();
        }

        private void radioButtonMe_MouseClick(object sender, MouseEventArgs e)
        {
            if (!radioButtonMe.Checked)
            {
                btnViewFriendInfos.Enabled = false;
                radioButtonShare.Checked = false;
                radioButtonMe.Checked = true;
            }
            else
            {
                btnViewFriendInfos.Enabled = false;
            }
        }

        private void radioButtonShare_MouseClick(object sender, MouseEventArgs e)
        {
            if (!radioButtonShare.Checked)
            {
                btnViewFriendInfos.Enabled = true;
                radioButtonShare.Checked = true;
                radioButtonMe.Checked = false;
            }
        }
    }
}
